﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Repast
{
    public partial class Frm_AddUnit : BaseXtraForm
    {
        //单位ID
        public Guid? UnitID { get; set; }

        public event EventHandler SaveEvent;

        public Frm_AddUnit(EStatus status,Guid? id = null)
        {
            InitializeComponent();
            this.FStatus = status;
            this.UnitID = id;
        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_AddUnit_Load(object sender, EventArgs e)
        {
            RepastService rservice = new RepastService();
            Unit unit = null;

            if (this.FStatus == EStatus.Add)
            {
                this.Text = "单位 - 添加";
            }
            else if (this.FStatus == EStatus.Modify)
            {
                unit = rservice.GetUnitByID(this.UnitID);

                this.NameEdit.Text = unit.Name;
                this.CodeEdit.Text = unit.Code;
                this.Text = "单位 - 编辑";
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_OK_Click(object sender, EventArgs e)
        {
            RepastService rservice = new RepastService();
            Unit unit = null;

            if (this.FStatus == EStatus.Add)
            {
                rservice.AddUnit(this.CodeEdit.Text.Trim(),this.NameEdit.Text.Trim());
                ShowInfo("添加成功!");
            }
            else if (this.FStatus == EStatus.Modify)
            {
                unit = rservice.UpdateUnit(this.CodeEdit.Text.Trim(), this.NameEdit.Text.Trim(), this.UnitID);
                ShowInfo("编辑成功!");                
            }

            if (SaveEvent != null)
            {
                SaveEvent(unit, new EventArgs());
            }
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Cancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
